let app = getApp();
Page({

  data: {
    allMovies : [],
    navigationBarTitle : '',
    categoryUrl : '',
    count : 10,
  },

  onLoad: function (options) {
    let json = {
      '正在热映': 'in_theaters',
      '即将上映': 'coming_soon',
      '豆瓣TOP250': 'top250'
    };
    let allCategoryUrl = json[options.categoryTitle];
    this.data.categoryUrl = allCategoryUrl;
    this.data.navigationBarTitle = options.categoryTitle;
    //2.All正在热映
    this.getMoreMovie(this.data.categoryUrl, this.data.count);
  },

  //1.get请求数据
  getMoreMovie(allCategoryUrl, count) { //总条数 : totalCount
    wx.request({
      url: app.globalData.doubanApi + allCategoryUrl + '?start=0&count=' + count,
      method: 'GET',
      header: {
        "content-type": "json"
      },
      success: (res) => {
        this.setData({
          allMovies: res.data.subjects  //20条
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      },
    })
  },

  //3.设置标题
  onReady : function(){
    wx.setNavigationBarTitle({
      title: this.data.navigationBarTitle,
    })
  },

  //4.滚动底部加载更多
  onReachBottom(){  
    console.log('加载更多');
    wx.showNavigationBarLoading();
    let totalCount = this.data.count += 10;
    this.getMoreMovie(this.data.categoryUrl, totalCount);
    
  },

  //下拉刷新
  onPullDownRefresh : function(e){
    this.setData({
      count: 0,
    });
    this.onReachBottom();
  },
})